package com.briup.smartcity_group1.service.impl;

import com.briup.smartcity_group1.bean.basic.Comment;
import com.briup.smartcity_group1.dao.basic.CommentMapper;
import com.briup.smartcity_group1.dao.ext.CommentExtMapper;
import com.briup.smartcity_group1.exception.ServiceException;
import com.briup.smartcity_group1.service.ICommentService;
import com.briup.smartcity_group1.utils.result.ResultCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 评论模块相关接口实现类
 * @author WangChen
 * @date 2023/1/6 2:34
 */
@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper mapper;
    @Autowired
    private CommentExtMapper extMapper;

    /**
     * 审核评论
     * 通过修改评论状态实现,状态：0-待审核，1-不通过，2-通过
     *
     * @param id     评论信息id
     * @param status 评论状态
     */
    @Override
    public void checkComment(int id, int status) {

        if (mapper.selectByPrimaryKey(id) == null){
            throw new ServiceException(ResultCode.COMMENT_NOT_FOUND);
        }

        if (status != 1 && status != 2 && status != 3){
            throw new ServiceException(ResultCode.COMMENT_UNEXPECTED_STATUS);

        }

        Comment comment = new Comment();
        comment.setCommentId(id);
        comment.setCommentStatus(status);

        mapper.updateByPrimaryKeySelective(comment);
    }

    /**
     * 分页查询所有评论
     * 通过关键词分页对查询评论模糊查询
     *
     * @param pageNum   页码
     * @param pageSize  页面大小
     * @param keywords  查询用关键字
     * @return 存放当前页评论信息的列表
     */
    @Override
    public PageInfo<Comment> pageQuery(int pageNum, int pageSize, String keywords) {
        PageInfo<Comment> pageInfo = PageHelper.startPage(pageNum, pageSize, true).doSelectPageInfo(
                () -> extMapper.selectAllByKeyWord(keywords)
        );
        return pageInfo;
    }

    /**
     * 通过id级联删除评论
     * 当评论信息作为父id时，删除评论信息需要将其对应的子评论信息删除
     *
     * @param id 评论信息id
     */
    @Override
    public void deleteById(int id) {
        //级联删除
        mapper.deleteByPrimaryKey(id);
    }

    /**
     * 通过数组批量删除多个评论信息
     *
     * @param ids   评论信息id
     * @throws ServiceException 待批处理数据中有部分数据不存在;
     *                          待批处理数据全不存在;
     */
    @Override
    public void batchDelete(int[] ids) {

        //未找到的数据条数
        int notFoundCount = 0;

        for (int id:
             ids) {
            if (mapper.selectByPrimaryKey(id) == null){
                notFoundCount++;
            }
        }

        extMapper.batchDeleteByPrimaryKey(ids);

        //部分数据未找到时抛出异常信息
        if (notFoundCount == 0){
            return;
        }
        else if (notFoundCount > 0 && notFoundCount < ids.length){
            throw new ServiceException(ResultCode.COMMENT_NOT_FOUND_BATCH_PARTLY);
        }else{
            throw new ServiceException(ResultCode.COMMENT_NOT_FOUND_BATCH_ALL);
        }
    }
}
